﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CutePager;
using QLTB.Data;
using QLTB.Entities;
using SiteUtils;

namespace QuanLyThietBi.DCTEDU_.BaoCao
{
    public partial class Tong_Hop_Bao_Cao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindData();

            }
        }

        private void BindData()
        {
            int totalRows = 0;
            string whereClause = "";
            TList<PhongHoc> TList_PhongHoc = DataRepository.PhongHocProvider.GetPaged(whereClause, "", 0, 0,
                out totalRows);
            rpPhongMay.DataSource = TList_PhongHoc;
            rpPhongMay.DataBind();
            rpPhongMayPhanMem.DataSource = TList_PhongHoc;
            rpPhongMayPhanMem.DataBind();
        }

        public string getTenCanBo(object ob)
        {
            string id = ob.ToString();
            return DataRepository.CanBoProvider.GetByMaCanBo(id).HoTen;
        }

        public int getSoThietBi(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            return DataRepository.ThietBiProvider.Find("MaPhong=" + id.ToString()).Count;
        }

        public int getSoPhanMem(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            return DataRepository.PhongHocPhanMemProvider.Find("MaPhong=" + id.ToString()).Count;
        }

        public string getDanhGiaPhanMem(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            TList<PhongHocPhanMem> TList_PHPM = DataRepository.PhongHocPhanMemProvider.Find("MaPhong=" + id.ToString());
            if (TList_PHPM.Count > 30)
            {
                return "<font color='red'>Quá tải</font>";
            }
            else
            {
                if (TList_PHPM.Count > 20)
                {
                    return "<font color='orange'>Tương đối nhiều</font>";
                }
                else
                {
                    if (TList_PHPM.Count > 10)
                    {
                        return "<font color='green'>Bình thường</font>";
                    }
                    else
                    {
                        return "<font color='brown'>Quá ít</font>";
                    }
                }
            }
            return "<font color='brown'>Quá ít</font>";
        }

        public int getSoLanSuaChua(object ob)
        {
            string id = ob.ToString();//Mã phòng-> Mã thiết bị
            int totalRows = 0;
            string whereClause = "";
            whereClause += "MaHoatDongSuaChua in (select MaHoatDongSuaChua from HoatDongSuaChua where MaThietBi in (Select MaThietBi from ThietBi where MaPhong = " + id + "))";
            TList<ChiTietHoatDongSuaChua> TList_CTHDSC =
                DataRepository.ChiTietHoatDongSuaChuaProvider.GetPaged(whereClause, "", 0, 0, out totalRows);
            return TList_CTHDSC.Count;
        }

        public int getSoThietBiBaoCao(object ob)
        {
            string id = ob.ToString();//Mã phòng-> Mã thiết bị
            int totalRows = 0;
            string whereClause = "";
            whereClause += "MaThietBi in (Select MaThietBi from ThietBi where MaPhong = " + id + ")";
            TList<HoatDongSuaChua> TList_HDSC =
                DataRepository.HoatDongSuaChuaProvider.GetPaged(whereClause, "", 0, 0, out totalRows);
            return TList_HDSC.Count;
        }

        public int getSoLanChoSuaChua(object ob)
        {
            string id = ob.ToString();//Mã phòng-> Mã thiết bị
            int totalRows = 0;
            string whereClause = "";
            whereClause += "MaHoatDongSuaChua not in (select MaHoatDongSuaChua from ChiTietHoatDongSuaChua) and MaThietBi in (Select MaThietBi from ThietBi where MaPhong = " + id + ")";
            TList<HoatDongSuaChua> TList_CTHDSC =
                DataRepository.HoatDongSuaChuaProvider.GetPaged(whereClause, "", 0, 0, out totalRows);
            return TList_CTHDSC.Count;
        }

        public int getPhanTramChoSuaChua(object ob)
        {
            int soLan;
            if (getSoLanSuaChua(ob) + getSoLanChoSuaChua(ob) == 0)
            {
                return 0;
            }
            else
            {
                int tong = getSoLanSuaChua(ob) + getSoLanChoSuaChua(ob);
                int sc = getSoLanChoSuaChua(ob);
                soLan = (sc * 100 / tong);
            }
            
            return soLan;
        }

        public int getPhanTramDaSuaChua(object ob)
        {
            int soLan;
            if (getSoLanSuaChua(ob) + getSoLanChoSuaChua(ob) == 0)
            {
                return 0;
            }
            else
            {
                int tong = getSoLanSuaChua(ob) + getSoLanChoSuaChua(ob);
                int sc = getSoLanSuaChua(ob);
                soLan = (sc * 100 / tong);
            }

            return soLan;
        }

        public string getDanhGiaSuaChua(object ob)
        {
            string id = ob.ToString();//Mã phòng-> Mã thiết bị
            int totalRows = 0;
            string whereClause = "";
            whereClause += "MaHoatDongSuaChua not in (select MaHoatDongSuaChua from ChiTietHoatDongSuaChua) and MaThietBi in (Select MaThietBi from ThietBi where MaPhong = " + id + ")";
            TList<HoatDongSuaChua> TList_CTHDSC =
                DataRepository.HoatDongSuaChuaProvider.GetPaged(whereClause, "", 0, 0, out totalRows);
            if (TList_CTHDSC.Count < getSoThietBi(ob) * 3)
            {
                return "Bình thường";
            }
            else
            {
                return "Hư hỏng nhiều";
            }
        }

        protected void rpPhongMay_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField b = e.Item.FindControl("H_MaPhong") as HiddenField;
                Repeater rp = e.Item.FindControl("rpThietBi") as Repeater;
                CollectionPager pager = e.Item.FindControl("pager") as CollectionPager;
                int totalRows = 0;
                string whereClause = "MaThietBi in (select MaThietBi from ThietBi where MaPhong = " + b.Value + ")";
                TList<HoatDongSuaChua> TList_HDSC = DataRepository.HoatDongSuaChuaProvider.GetPaged(whereClause,
                    "NgayCapNhat     DESC", 0, 0, out totalRows);

                //Set DataSource of Pager to Sample Data 
                pager.DataSource = TList_HDSC;

                pager.BindToControl = rp;

                rp.DataSource = pager.DataSourcePaged;
                //rp.DataSource = TList_HDSC;
                rp.DataBind();
                // Pager Pager1 = e.Item.FindControl("Pager1") as Pager;

                // DataSet dsHoatDong = DataRepository.Provider.ExecuteDataSet("HoatDongSuaChua_GetPaged", whereClause, "NgayCapNhat DESC",
                //1 - 1, Pager1.PageSize);
                // rp.DataSource = dsHoatDong.Tables[0];
                // rp.DataBind();
                // try
                // {
                //     totalRows = Convert.ToInt32(dsHoatDong.Tables[1].Rows[0][0]);
                // }
                // catch (Exception)
                // {
                // }
                // Pager1.ItemCount = totalRows;
                // if (totalRows <= Pager1.PageSize)
                //     Pager1.Visible = false;
                // else
                //     Pager1.Visible = true;


            }
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            //Pager1.CurrentIndex = currnetPageIndex;
            //BindHoatDong(currnetPageIndex);
        }

        protected void cmdPrint_OnClick(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), "onclick", "PrintDiv()", true);
            Page_Load(sender, e);
        }
        public string getTenThietBi(object ob)
        {
            string id = ob.ToString();
            return DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id)).TenThietBi;
        }

        public string getNgaySuaChua(object ob)
        {
            string id = ob.ToString();
            ChiTietHoatDongSuaChua ent_CTHDSC = DataRepository.ChiTietHoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(Convert.ToInt32(id));
            if (ent_CTHDSC != null)
            {
                return Convert.ToDateTime(ent_CTHDSC.NgaySuaChua).ToString("dd/MM/yyyy");
            }
            else
            {
                return "<center><img src='../../Images/redx.png' /></center>";
            }
        }

        public string getHuongGiaiQuyet(object ob)
        {
            string id = ob.ToString();
            ChiTietHoatDongSuaChua ent_CTHDSC = DataRepository.ChiTietHoatDongSuaChuaProvider.GetByMaHoatDongSuaChua(Convert.ToInt32(id));
            if (ent_CTHDSC != null)
            {
                return ent_CTHDSC.HuongGiaiQuyet;
            }
            else
            {
                return "<center><img src='../../Images/redx.png' /></center>";
            }
        }

        public int getSoLanSuaChuaThietBi(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            TList<HoatDongSuaChua> TList_HDSC = DataRepository.HoatDongSuaChuaProvider.Find("MaThietBi=" + id);
            return TList_HDSC.Count;
        }

    }
}